package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdClientMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdClient;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdClientService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdClientVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-03
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.MdClientServiceImpl")
public class MdClientServiceImpl extends ServiceImpl<MdClientMapper, MdClient> implements MdClientService {

    private static final Logger logger = LoggerFactory.getLogger(MdClientServiceImpl.class);
    
    
    @Autowired
    private MdClientService mdClientService;
    
    @Autowired
    private MdClientMapper mdClientMapper;
    
    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-客户表-开始同步客户表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的客户表信息
        List<MdClientVO> allClients = mdClientMapper.selectAllClient();
        // 2客户表信息转换成主键-对象map集合
        Map<Long, MdClientVO> allClientsMap = allClients.stream().collect(Collectors.toMap(MdClientVO::getClientId, t -> t));
        // 3客户表信息中的id转换成id列表
        List<Long> erpAllClientIdList = allClients.stream().map(MdClientVO::getClientId).collect(Collectors.toList());
        // 4查询所有的mes客户表信息
        QueryWrapper<MdClient> mdClientWrapper = new QueryWrapper<>();
        mdClientWrapper.eq("deleted_flag","0");
        List<MdClient> mesMdClients = mdClientMapper.selectList(mdClientWrapper);
        // 5mes中的客户信息转换成id列表,
        List<Long> mesIdList = mesMdClients.stream().map(MdClient::getErpClientId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllClientIdList.size(); i++) {
            if (!mesIdList.contains(erpAllClientIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllClientIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<MdClient> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                MdClientVO mdClientVO = allClientsMap.get(insertId);
                MdClient mdClient = new MdClient();
                mdClient.setClientId(mdClientVO.getClientId());
                mdClient.setClientCode(mdClientVO.getClientCode());     // 客户编码
                mdClient.setClientName(mdClientVO.getClientName());     // 客户名称
                mdClient.setEnableFlag(mdClientVO.getEnableFlag());     // 是否启用
                mdClient.setScheduleTime(nowTime);
                mdClient.setCreateBy(mdClientVO.getCreateBy());
                mdClient.setCreateTime(mdClientVO.getCreateTime());
                mdClient.setUpdateBy(mdClientVO.getUpdateBy());
                mdClient.setUpdateTime(mdClientVO.getUpdateTime());
                mdClient.setErpClientId(mdClientVO.getClientId());
                insertList.add(mdClient);
            }
            mdClientService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<Long> updateIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllClientIdList.contains(mesIdList.get(i))){
                updateIdList.add(mesIdList.get(i));
            }
        }
        if (updateIdList.size()>0) {
            ArrayList<MdClient> mdClientUpdateList = new ArrayList<>();
            List<MdClient> mdClientList = mdClientMapper.selectBatchIds(updateIdList);
            for (MdClient mdClient : mdClientList) {
                Long erpClientId = mdClient.getErpClientId();
                MdClientVO mdClientVO = allClientsMap.get(erpClientId);
                mdClient.setClientCode(mdClientVO.getClientCode());     // 客户编码
                mdClient.setClientName(mdClientVO.getClientName());     // 客户名称
                mdClient.setEnableFlag(mdClientVO.getEnableFlag());     // 是否启用
                mdClient.setScheduleTime(nowTime);
                mdClient.setCreateBy(mdClientVO.getCreateBy());
                mdClient.setCreateTime(mdClientVO.getCreateTime());
                mdClient.setUpdateBy(mdClientVO.getUpdateBy());
                mdClient.setUpdateTime(mdClientVO.getUpdateTime());
                mdClientUpdateList.add(mdClient);
            }
            mdClientService.updateBatchById(mdClientUpdateList);
        }
/*        // 8找出erp中没有，mes中有的，这些需要删除
        ArrayList<Long> erpNoMesHasIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (!erpAllClientIdList.contains(mesIdList.get(i))){
                erpNoMesHasIdList.add(mesIdList.get(i));
            }
        }
        if (erpNoMesHasIdList.size()>0) {
            mdClientMapper.updateIsDeleteById(erpNoMesHasIdList);
        }*/
    }
}
